define(function () {
    'use strict';

    function ctrl($scope,ConcernService,CommonService,InfoService) {

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        function init(){
            $scope.userCareFunds = new Array();
            $scope.otherFunds = new Array();

            //获取推荐关注列表
            othersCareList();
            //用户关注列表
            userCareList();
        }

        function userCareList(){
            var prodCodes= ConcernService.getConcernList();
            $scope.concernSize = prodCodes.length;
            if(prodCodes != "") {
                InfoService.getProdBriefs(prodCodes).then(
                    function(data){
                        if(data){
                            $scope.userCareFunds = data;
                            perfectFunds($scope.userCareFunds);
                        }
                    }
                );
            }
        }

        function othersCareList(){
            InfoService.getOthersCare().then(
                function(data){
                    if(data){
                        $scope.otherFunds = data;
                        perfectFunds( $scope.otherFunds);
                    }
                }
            );
        }

        $scope.unCare = function(prod_code){
            ConcernService.concern(prod_code);
            var list = $scope.userCareFunds;
            for(var i=0;i<list.length;i++){
                if(prod_code == list[i].prod_code){
                    list.splice(i,1);
                    $scope.concernSize = list.length;
                }
            }
        };

        function perfectFunds(funds){
            for(var i=0;i < funds.length; i++) {
                var fund = funds[i];
                fund.url = CommonService.productPageUrl(fund.prod_profit_mode, fund.prod_source, fund.prod_catalog_mark);
            }
        }
    }

    ctrl.$inject = ['$scope','ConcernService','CommonService','InfoService'];
    return ctrl;
});